package jgl.vajra.operator.part.order.activity;

import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.fastjson.JSONObject;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.GrabOrdersBean;
import jgl.vajra.operator.databinding.ActivityGrabDetailsBinding;
import jgl.vajra.operator.event.GrabDetailsEvent;
import jgl.vajra.operator.event.GrabOrderModifyEvent;
import jgl.vajra.operator.event.OrderDetailsCancelEvent;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.netty.GrabOrderBean;
import jgl.vajra.operator.netty.MessageBody;
import jgl.vajra.operator.netty.MessageOrderBean;
import jgl.vajra.operator.part.order.contract.GrabDetailsContract;
import jgl.vajra.operator.part.order.viewModel.GrabDetailsViewModel;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.utils.TimeUtils;
import jgl.vajra.operator.widget.utils.ToastUtils;

@CreateViewModel(GrabDetailsViewModel.class)
public class GrabDetailsActivity extends BaseMVVMActivity<GrabDetailsViewModel, ActivityGrabDetailsBinding> implements GrabDetailsContract.View, BaseBindingItemPresenter<String> {

    private long maxUseCarTime;
    private ArrayList mList;
    private SingleTypeBindingAdapter adapter;
    private GrabOrderBean data;

    @Override
    public int getLayoutId() {
        return R.layout.activity_grab_details;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        data = (GrabOrderBean) getIntent().getSerializableExtra("itemData");
        mBinding.setData(data);

        Map<String, String> picture = data.picture;
        mList = new ArrayList();
        for (Map.Entry<String, String> entity : picture.entrySet()) {
            String value = entity.getValue();
            mList.add(value);
        }

        if (mList.size()==0){
            mBinding.llPicture.setVisibility(View.GONE);
        }else {
            mBinding.llPicture.setVisibility(View.VISIBLE);
        }

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        adapter = new SingleTypeBindingAdapter(this, mList, R.layout.item_grab_details);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        int delaySecond=31000;
        if (!TextUtils.isEmpty(data.delaySecond)){
            delaySecond = Integer.parseInt(data.delaySecond)*1000;
        }
        maxUseCarTime = data.countDownStart + delaySecond;
        long currentTime = System.currentTimeMillis();

        if (currentTime < maxUseCarTime) {
            long time = (maxUseCarTime - currentTime) / 1000;
            mBinding.countDownView.setVisibility(View.VISIBLE);
            mBinding.countDownView.setCountdownTime((int) time);
            mBinding.countDownView.setOnCountDownListener(() -> {
                mBinding.countDownView.setVisibility(View.GONE);
            });
            mBinding.countDownView.startCountDown();
        }
    }

    @Override
    public void onGrabOrders(GrabOrderBean data) {
        long currentTime = System.currentTimeMillis();
        if (currentTime < maxUseCarTime) {
            long time = (maxUseCarTime - currentTime) / 1000;
            ToastUtils.showShort(time + "秒后才能抢单");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("orderId", data.orderId);
        map.put("logo", data.logo);
        //map.put("driverInvitation", data.driverInvitation);
        map.put("amount", data.newAmount);
        map.put("udid", data.udid);
        mViewModel.getGrabOrdersData(map);
    }

    @Override
    public void returnGrabOrdersData(GrabOrdersBean data) {
        ToastUtils.showShort(data.reason);
        if (data.isGrabOrders) {
            IntentManager.toOrderDetailsActivity(this, data.orderId);
        }
        EventBus.getDefault().post(new GrabDetailsEvent(data));
        finish();
    }

    //点击图片
    @Override
    public void onItemClick(int position, String itemData) {
        IntentManager.toBigImageNetActivity(this, mList, position);
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOrderDetailsCancelEvent(OrderDetailsCancelEvent event) {
        MessageBody messageInfo = JSONObject.parseObject(event.content, MessageBody.class);
        if (String.valueOf(messageInfo.orderId).equals(data.orderId)) {
            finish();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onGrabOrderModifyEvent(GrabOrderModifyEvent event) {
        String content = event.content;
        MessageOrderBean beanMessage = JSONObject.parseObject(content, MessageOrderBean.class);
        if (String.valueOf(beanMessage.orderId).equals(data.orderId)) {
            data.newAmount = beanMessage.amount;
            mBinding.setData(data);
        }
    }
}
